//
//  SISSUserInfoDAL.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/3/12.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSUserInfoDAL.h"
#import "SISSSQLiteManager.h"

static NSString *const tableName_ = @"t_userInfo";

static const NSTimeInterval  maxTime_ = -7 * 24 * 3600;
@implementation SISSUserInfoDAL

+ (void)cachesUserInfo:(NSMutableDictionary *)userInfo {
    if (SISSIsEmpty(userInfo)) {
        return;
    }
    
    NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO t_userInfo(spId, userInfo) VALUES(?, ?)"];
    
    [[SISSSQLiteManager sharedManager].dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        @try {
            
            NSData *userInfoData = [NSJSONSerialization dataWithJSONObject:userInfo options:0 error:nil];
            
            if (![db executeUpdate:sql withArgumentsInArray:@[userInfo[@"spId"], userInfoData]]) {
                *rollback = YES;
            }
            
        } @catch (NSException *exception) {
            
            NSLog(@"存储错误: %@", exception);
        } @finally {
            
            //                *stop = YES;
            //                *rollback = YES;
            
        }
        
        NSLog(@"向数据库新增%d条数据", db.changes);
        
    }];
 
}


+ (void)queryUserInfoFromDiskCompletion:(void (^)(NSMutableDictionary *))completion {
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE spId = %@", tableName_,[SISSUserAccount shared].spId];
   
    
    [[SISSSQLiteManager sharedManager] queryArrayOfDicts:sql completion:^(NSMutableArray<NSMutableDictionary *> *dictArrayM) {
        
        __block NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
        
        [dictArrayM enumerateObjectsUsingBlock:^(NSMutableDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSData *userInfoData = obj[@"userInfo"];
            
            @try {
                NSDictionary *userInfoDict = [NSJSONSerialization JSONObjectWithData:userInfoData options:NSJSONReadingMutableLeaves error:nil];
                
                dictM = userInfoDict.mutableCopy;
                
            } @catch (NSException *exception) {
                
            } @finally {
                
                
            }
            
        }];
        
        completion(dictM);
        
    }];
    
    
}

+ (void)clearOutTimeCashes {
    NSDate *earlyTime = [NSDate dateWithTimeIntervalSinceNow:maxTime_];
    
    NSString *earlyTimeStr = [earlyTime yyr_stringWithFormat:@"yyyy-MM-dd HH:mm:ss" timeZone:nil locale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
    
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE time < '%@'", tableName_, earlyTimeStr];
    
    [[SISSSQLiteManager sharedManager].dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        
        BOOL isSucceed = [db executeUpdate:sql withArgumentsInArray:@[]];
        
        if (isSucceed) {
            NSLog(@"%d 删除数据成功", db.changes);
            *rollback = NO;
        }else
        {
            *rollback = YES;
        }
        
    }];
    
}



@end
